//
//  ChannelComponent.m
//  ChannelCommonComponent
//
//  Created by LiuLian on 14-2-18.
//  Copyright (c) 2014年 LiuLian. All rights reserved.
//

#import "ChannelComponent.h"
#import "CLGloble.h"
#import "NSObject+kit.h"
#import "CLNavigationController.h"
#import "NSObject+sagittarius.h"

@implementation ChannelComponent

+ (ChannnelTopScrollView *)onlyTopScrollView
{
//    cactusAppDelegate *delegate = (cactusAppDelegate *)[UIApplication sharedApplication].delegate;
//    return delegate.appRootViewController.topScrollView;
    return ((CLNavigationController *)[self k_rootController]).mainController.topScrollView;
}

+ (ChannnelControllerScrollView *)onlyControllerScrollView
{
//    cactusAppDelegate *delegate = (cactusAppDelegate *)[UIApplication sharedApplication].delegate;
//    return delegate.appRootViewController.controllerScrollView;
    return ((CLNavigationController *)[self k_rootController]).mainController.controllerScrollView;
}

+ (NSInteger)currentIndex
{
    NSInteger current = [self onlyTopScrollView].currentButtonTag-100;
    if (current < 0) {
        return -1;
    }
    else
        return current;
}

+ (NSDictionary *)currentDictionaryWithIndex:(NSInteger)index
{
    return [[self onlyTopScrollView].channelData sgrGetDictionaryForIndex:index];
}

+ (void)updateButtonStateWithTag:(NSInteger)tag
{
    [self onlyTopScrollView].currentButtonTag = tag;
    
    for (UIButton *button in [self onlyTopScrollView].buttons) {
        button.selected = NO;
    }
    
    UIButton *selectedButton = (UIButton *)[[self onlyTopScrollView] viewWithTag:tag];
    selectedButton.selected = YES;
    [[self onlyTopScrollView] scrollRectToVisible:selectedButton.frame animated:YES];
    
    [[ChannelComponent onlyControllerScrollView] setContentOffset:CGPointMake((tag-100)*[ChannelComponent onlyControllerScrollView].frame.size.width, 0) animated:NO];
    [ChannelComponent updateDataSource];
}

+ (void)updateDataSource
{
    if ([self onlyControllerScrollView].lastCurrentIndex == [ChannelComponent currentIndex]) {     //未更换频道
        return;
    }
    else if (fabs([self onlyControllerScrollView].lastCurrentIndex - [ChannelComponent currentIndex]) == 1) {  //更换相邻频道
        //更新移动后位置
        [self updateChildViewWithVisibleState:[self onlyControllerScrollView].visibleState WithMoveState:ScrollViewMoveStateSlow];
    }
    else if (fabs([self onlyControllerScrollView].lastCurrentIndex - [ChannelComponent currentIndex]) > 1) {
        [self updateChildViewWithVisibleState:ScrollViewVisibleStateCurrent WithMoveState:ScrollViewMoveStateFast];
    }
    else {
        NSLog(@"未知错误");
    }
    
    [self onlyControllerScrollView].lastCurrentIndex = [ChannelComponent currentIndex];
}

//判断是否为用户当前所见Controller
+ (void)updateDataSourceWithInCurrent
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"channelChanged" object:nil];
    
    for (UIViewController *controller in [self onlyControllerScrollView].child) {
        [self onlyControllerScrollView].channelDelegate = controller;
        if ([self onlyControllerScrollView].channelDelegate && [[self onlyControllerScrollView].channelDelegate respondsToSelector:@selector(updateControllerWithIsCurrent:)]) {
            [[self onlyControllerScrollView].channelDelegate updateControllerWithIsCurrent:(controller.view.frame.origin.x==[self onlyControllerScrollView].contentOffset.x)];
        }
    }
}

+ (void)updateChildViewWithVisibleState:(ScrollViewVisibleState)visibleState WithMoveState:(ScrollViewMoveState)moveState
{
    [self onlyControllerScrollView].directionX = [self onlyControllerScrollView].lastCurrentIndex*320;
    BOOL isLeft = [self getDirection];
    switch (moveState) {
        case ScrollViewMoveStateSlow:
            [self updateChildViewWithVisibleState:visibleState WithMoveDirection:isLeft];
            break;
        case ScrollViewMoveStateFast: {
            for (NSInteger i = 0; i < [[self onlyControllerScrollView].child count]; i++) {
                UIViewController *controller = [[self onlyControllerScrollView].child objectAtIndex:i];
                [controller.view setFrame:CGRectMake(([ChannelComponent currentIndex]+(i-1))*[self onlyControllerScrollView].frame.size.width, 0, GlobleWidth, [self onlyControllerScrollView].frame.size.height)];
                
                switch (i) {
                    case 0:
                        [self updateSourceWithController:controller
                                               WithIndex:([ChannelComponent currentIndex]+(i-1))
                                        WithVisibleState:ScrollViewVisibleStateBefore
                                           WithIsCurrent:NO];
                        break;
                    case 1:
                        [self updateSourceWithController:controller
                                               WithIndex:([ChannelComponent currentIndex]+(i-1))
                                        WithVisibleState:ScrollViewVisibleStateCurrent WithIsCurrent:YES];
                        break;
                    case 2:
                        [self updateSourceWithController:controller
                                               WithIndex:([ChannelComponent currentIndex]+(i-1))
                                        WithVisibleState:ScrollViewVisibleStateNext WithIsCurrent:NO];
                        break;
                        
                    default:
                        break;
                }
                
                
            }
            [self onlyControllerScrollView].visibleState = ScrollViewVisibleStateCurrent;
        }
            break;
            
        default:
            break;
    }
}

+ (void)updateChildViewWithVisibleState:(ScrollViewVisibleState)visibleState
                      WithMoveDirection:(BOOL)isLeft
{
    switch (visibleState) {
        case ScrollViewVisibleStateBefore: {
            UIViewController *controller = [[self onlyControllerScrollView].child objectAtIndex:(isLeft?2:1)];
            [controller.view setFrame:CGRectMake(([ChannelComponent currentIndex]+(isLeft?1:-1))*[self onlyControllerScrollView].frame.size.width, 0, GlobleWidth, [self onlyControllerScrollView].frame.size.height)];
            
            [self onlyControllerScrollView].visibleState = isLeft?ScrollViewVisibleStateCurrent:ScrollViewVisibleStateNext;
            [self updateSourceWithController:controller
                                   WithIndex:([ChannelComponent currentIndex]+(isLeft?1:-1))
                            WithVisibleState:[self onlyControllerScrollView].visibleState WithIsCurrent:NO];
        }
            break;
        case ScrollViewVisibleStateCurrent: {
            UIViewController *controller = [[self onlyControllerScrollView].child objectAtIndex:(isLeft?0:2)];
            [controller.view setFrame:CGRectMake(([ChannelComponent currentIndex]+(isLeft?1:-1))*[self onlyControllerScrollView].frame.size.width, 0, GlobleWidth, [self onlyControllerScrollView].frame.size.height)];
            
            [self onlyControllerScrollView].visibleState = isLeft?ScrollViewVisibleStateNext:ScrollViewVisibleStateBefore;
            [self updateSourceWithController:controller
                                   WithIndex:([ChannelComponent currentIndex]+(isLeft?1:-1))
                            WithVisibleState:[self onlyControllerScrollView].visibleState WithIsCurrent:NO];
        }
            break;
        case ScrollViewVisibleStateNext: {
            UIViewController *controller = [[self onlyControllerScrollView].child objectAtIndex:(isLeft?1:0)];
            [controller.view setFrame:CGRectMake(([ChannelComponent currentIndex]+(isLeft?1:-1))*[self onlyControllerScrollView].frame.size.width, 0, GlobleWidth, [self onlyControllerScrollView].frame.size.height)];
            
            [self onlyControllerScrollView].visibleState = isLeft?ScrollViewVisibleStateBefore:ScrollViewVisibleStateCurrent;
            [self updateSourceWithController:controller
                                   WithIndex:([ChannelComponent currentIndex]+(isLeft?1:-1))
                            WithVisibleState:[self onlyControllerScrollView].visibleState WithIsCurrent:NO];
        }
            break;
            
        default:
            break;
    }
}

//判断左滑or又滑
+ (BOOL)getDirection
{
    static float temp_x = 0;
    temp_x = [self onlyControllerScrollView].contentOffset.x;
    
    if (temp_x != [self onlyControllerScrollView].directionX) {
        if (temp_x >= [self onlyControllerScrollView].directionX) {
            [self onlyControllerScrollView].directionX = temp_x;
            return YES;
        }
        else {
            [self onlyControllerScrollView].directionX = temp_x;
            return NO;
        }
    }
    else
        return YES;
}

+ (void)updateSourceWithController:(UIViewController *)controller
                         WithIndex:(NSInteger)index
                  WithVisibleState:(ScrollViewVisibleState)state
                     WithIsCurrent:(BOOL)isCurrent
{
    [self onlyControllerScrollView].channelDelegate = controller;
    if ([self onlyControllerScrollView].channelDelegate && [[self onlyControllerScrollView].channelDelegate respondsToSelector:@selector(updateControllerWithDataSource:)]) {
        ChannelControllerDataSource *dataSource = [ChannelControllerDataSource new];
        dataSource.index = index;
        dataSource.controllerVisibleState = state;
        dataSource.isCurrentView = isCurrent;
        [[self onlyControllerScrollView].channelDelegate updateControllerWithDataSource:dataSource];
    }
}

+ (void)colorGradientWithWillBlackTag:(NSInteger)black WithWillRedTag:(NSInteger)red WithNumber:(NSInteger)number
{
    UIButton *willBlackButton = (UIButton *)[[self onlyTopScrollView] viewWithTag:black];
    UIButton *willRedButton = (UIButton *)[[self onlyTopScrollView] viewWithTag:red];
    // 117 121 132
    // 249 38 48
    NSInteger c_red = 132;
    NSInteger c_green = -83;
    NSInteger c_blue = -84;
    
    
    [willBlackButton setTitleColor:[UIColor colorWithRed:(249-c_red*number/GlobleWidth)/255.0 green:(38-c_green*number/GlobleWidth)/255.0 blue:(48-c_blue*number/GlobleWidth)/255.0 alpha:1.0] forState:UIControlStateNormal];
    [willBlackButton setTitleColor:[UIColor colorWithRed:(249-c_red*number/GlobleWidth)/255.0 green:(38-c_green*number/GlobleWidth)/255.0 blue:(48-c_blue*number/GlobleWidth)/255.0 alpha:1.0] forState:UIControlStateSelected];
    
    [willRedButton setTitleColor:[UIColor colorWithRed:(117+c_red*number/GlobleWidth)/255.0 green:(121+c_green*number/GlobleWidth)/255.0 blue:(132+c_blue*number/GlobleWidth)/255.0 alpha:1.0] forState:UIControlStateNormal];
    [willRedButton setTitleColor:[UIColor colorWithRed:(117+c_red*number/GlobleWidth)/255.0 green:(121+c_green*number/GlobleWidth)/255.0 blue:(132+c_blue*number/GlobleWidth)/255.0 alpha:1.0] forState:UIControlStateSelected];
    
}

+ (void)clearTitleWithTag:(NSInteger)tag
{
    for (UIButton *button in [self onlyTopScrollView].buttons) {
        [button setTitleColor:RGB(0x5e, 0x64, 0x75) forState:UIControlStateNormal];
        [button setTitleColor:RGB(0x5e, 0x64, 0x75) forState:UIControlStateSelected];
    }
    
    UIButton *selectedButton = (UIButton *)[[self onlyTopScrollView] viewWithTag:tag];
    [selectedButton setTitleColor:RGB(0xf5, 0x43, 0x43) forState:UIControlStateNormal];
    [selectedButton setTitleColor:RGB(0xf5, 0x43, 0x43) forState:UIControlStateSelected];
}

@end
